package leedcode_path.回溯;

import java.util.*;

public class 全排列2 {
    /*
    * 输入：nums = [1,1,2]
输出：
[[1,1,2],
 [1,2,1],
 [2,1,1]]*/
    List<Integer> path = new ArrayList<>();
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> permuteUnique(int[] nums) {
        boolean[] used=new boolean[nums.length];
        Arrays.fill(used,false);
        backtracking(nums,used);
        return res;
    }
    public void backtracking(int[] nums,boolean[] used){
        if (path.size()==nums.length){
            res.add(new ArrayList<>(path));
            return;
        }
        HashSet<Integer> setused = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if (used[i]==true){
                continue;
            }
            if (setused.contains(nums[i])){
                continue;
            }
            used[i]=true;
            path.add(nums[i]);
            setused.add(nums[i]);
            backtracking(nums,used);
            path.remove(path.size()-1);
            used[i]=false;

        }

    }
}
